home *** CD-ROM | disk | FTP | other *** search
- Path: news.cs.utah.edu!usenet
- From: bendixen@eng.utah.edu (Mason Bendixen)
- Newsgroups: comp.lang.c++
- Subject: Problems with inheritance and templates in g++
- Date: 8 Jan 1996 15:46:23 GMT
- Organization: University of Utah Computer Science Department
- Message-ID: <4cre8f$bkp@magus.cs.utah.edu>
- Reply-To: bendixen@eng.utah.edu
- NNTP-Posting-Host: cadesm48.eng.utah.edu
-
- Hi,
- During the link stage, g++ complains about
- undefined symbols that correspond to unused member
- functions of instantiated objects. The code in question
- compiles fine under Borland C++ 4.5 but not g++.
- Take the sample file:
-
- // BEGIN SAMPLE PROGRAM
-
- #include <iostream.h>
-
- template <class T>
- class base {
- public:
- base () {}
- virtual int a() = 0;
- virtual int b() = 0;
- static base<T>* make();
- };
-
- template <class T>
- class test : public base<T> {
- public:
- test() {}
- int a();
- int b();
- };
-
- template <class T>
- base<T>* base<T>::make() {return new test<T>;}
-
- template <class T>
- int test<T>::a() {return 1;}
-
- template <class T>
- int test<T>::b() {return 2;}
-
- int main() {
- test<int> b;
- //int i = b.a();
- //int j = b.b();
- base<int>* t = base<int>::make();
- cout << t->a() << endl;
- return 0;
- }
-
- // END SAMPLE PROGRAM
-
-
- When I try to compile I get:
-
- >> 97 cadesm48% g++ -o tester temp.C
- >> collect2: ld returned 2 exit status
- >> ld: Undefined symbol
- >> test<int>::a(void)
- >> test<int>::b(void)
-
- If I uncomment the following lines:
-
- >> int i = b.a();
- >> int j = b.b();
-
- The program will work just fine.
- Can I force g++ to instantiate all member of every
- instantiated object? I think this has something to
- do with the way g++ handles inheritance for templatized
- classes. Any suggestions, insight, or possible work
- a rounds in this age of pre- standardized C++ would be
- greatly appreciated.
-
- Thanks
- Mason Bendixen
-
-
-